import { fileURLToPath, URL } from 'node:url';

import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import IconsResolver from 'unplugin-icons/resolver';
import Icons from 'unplugin-icons/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { defineConfig } from 'vite';
import ViteMonacoPlugin from 'vite-plugin-monaco-editor';
import WindiCSS from 'vite-plugin-windicss';

import { visualizer } from 'rollup-plugin-visualizer';
import viteCompression from 'vite-plugin-compression';
// import path from 'path';

// import sftpUploader from 'sftp-uploader';

// const sftpConfig = {
//   dir: path.join(__dirname, 'dist/'), // 需要上传文件的目录
//   url: '/www/dist', // 上传到的目录
//   host: '119.45.240.39', // sftp地址
//   port: '21', // sftp端口
//   username: 'root', // 账号
//   password: 'Zt04985807', // 密码
//   // 延迟上传时间（毫秒），解决部分项目会触发多次打包完成的问题
//   delay: 0,
// };
// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  // 将server.hmr.overlay设置为false
  server: {
    open: true,

    hmr: {
      overlay: false,
    },
    proxy: {
      '/5001': {
        target: 'https://zhangteng.cloud:5001',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/5001/, ''),
      },
      '/4004': {
        target: 'http://119.45.240.39:4004',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/4004/, ''),
      },
      '/jinrishici': {
        target: 'https://v1.jinrishici.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/jinrishici/, ''),
      },
      '/geoapi-qweather': {
        target: 'https://geoapi.qweather.com/v2',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/geoapi-qweather/, ''),
      },
      '/devapi-qweather': {
        target: 'https://devapi.qweather.com/v7',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/devapi-qweather/, ''),
      },
      '/yzam': {
        target: 'https://api.moonshot.cn/v1',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/yzam/, ''),
      },
      '/docker': {
        target: 'http://119.45.240.39:4375/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/docker/, ''),
      },
    },
  },
  plugins: [
    WindiCSS(),
    vue(),

    visualizer({ open: true }),
    viteCompression(),
    AutoImport({
      eslintrc: {
        enabled: true,
      },
      imports: ['vue', 'vue-router', '@vueuse/core'],
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
        IconsResolver({
          prefix: 'Icon',
        }),
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
        IconsResolver({
          enabledCollections: ['ep'],
        }),
      ],
    }),
    Icons({
      autoInstall: true,
    }),
    ViteMonacoPlugin({}),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
  build: {
    assetsDir: './static',
    cssCodeSplit: true,
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ['console.log'],
      },
      output: {
        // 去掉注释内容
        comments: true,
      },
    },
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        // manualChunks(id) {
        //   if (id.includes('node_modules')) {
        //     return id.toString().split('node_modules/')[1].split('/')[0].toString();
        //   }
        // },
        vue: ['vue', 'vue-router', 'vuex'],
        chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
        entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
        assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体，图片等
      },
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
});

// npm i -D @commitlint/cli @commitlint/config-conventional @vitejs/plugin-vue autoprefixer cz-git eslint eslint-config-airbnb-base eslint-config-prettier eslint-define-config eslint-plugin-import eslint-plugin-prettier eslint-plugin-vue husky lint-staged postcss-html postcss-scss prettier stylelint stylelint-config-standard-scss stylelint-config-html stylelint-config-prettier-scss stylelint-config-recess-order stylelint-config-standard stylelint-order unplugin-auto-import unplugin-vue-components vite vue-eslint-parser sass

